package com.wbhz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wbhz.entity.Collect;
import com.wbhz.entity.Like;
import com.wbhz.entity.PageBean;
import com.wbhz.entity.Post;
import com.wbhz.entity.Reply;
import com.wbhz.entity.Tab;
import com.wbhz.entity.Vip;
import com.wbhz.mapper.PostMapper;
import com.wbhz.service.PostService;
import com.wbhz.util.MessageUtil;

@Service
public class PostServiceImpl implements PostService {
	Logger logger = Logger.getLogger(PostServiceImpl.class);
	
	@Autowired
	private PostMapper postMapper;

	public int insertPost(Post post) {
		logger.info("***PostServiceImpl类,insertPost的方法***");
		return postMapper.insertPost(post);
	}

	public List<Tab> selectTab() {
		logger.info("***PostServiceImpl类,selectTab的方法***");
		return postMapper.selectTab();
	}

	/**
	 * 分页显示书籍
	 */
	public PageBean<Post> selectPostByPage(String title, int currentPage) {
		logger.info("***PostServiceImpl类,selectPostByPage的方法***");
		HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Post> pageBean = new PageBean<Post>();
		
        //设置当前页数
        pageBean.setCurrPage(currentPage);
        
        //设置每页显示的数据
        int pageSize = MessageUtil.PAGE_SIZE;
        pageBean.setPageSize(pageSize);
        
        //设置总页数
        int totalCount = postMapper.selectCount();
        pageBean.setTotalCount(totalCount);
		
        //设置总页数
        double tc = totalCount;
        Double num = Math.ceil(tc/pageSize);
        pageBean.setTotalPage(num.intValue());
        
        map.put("start", (currentPage-1)*pageSize);
        map.put("size", pageBean.getPageSize());
        if (null == title) {
        	map.put("title", "");
		}else {
			map.put("title", title);
		}
        
        //封装每页显示的数据
        List<Post> postList = postMapper.selectByPage(map);
        pageBean.setLists(postList);
        
		return pageBean;
	}

	public List<Tab> selectTabByPost() {
		logger.info("***PostServiceImpl类,selectTabByPost的方法***");
		return postMapper.selectTabByPost();
	}

	public Vip selectVip(int userId) {
		logger.info("***PostServiceImpl类,selectVip的方法***");
		return postMapper.selectVip(userId);
	}

	public int updateStickPost(int postId, String stickOrNot) {
		logger.info("***PostServiceImpl类,updateStickPost的方法***");
		if (stickOrNot.equals("yes")) {
			return postMapper.updateStickPostY(postId);
		}else if (stickOrNot.equals("no")) {
			return postMapper.updateStickPostN(postId);
		}
		return 0;
	}

	public int insertReply(Reply reply) {
		logger.info("***PostServiceImpl类,insertReply的方法***");
		return postMapper.insertReply(reply);
	}

	public PageBean<Reply> selectReplyByPage(int replyPostId, int currentPage) {
		logger.info("***PostServiceImpl类,selectReplyByPage的方法***");
		HashMap<String,Object> map = new HashMap<String,Object>();
        PageBean<Reply> pageBean = new PageBean<Reply>();
		
        //设置当前页数
        pageBean.setCurrPage(currentPage);
        
        //设置每页显示的数据
        int pageSize = MessageUtil.PAGE_SIZE;
        pageBean.setPageSize(pageSize);
        
        //设置总页数
        int totalCount = postMapper.selectCount();
        pageBean.setTotalCount(totalCount);
		
        //设置总页数
        double tc = totalCount;
        Double num = Math.ceil(tc/pageSize);
        pageBean.setTotalPage(num.intValue());
        
        map.put("start", (currentPage-1)*pageSize);
        map.put("size", pageBean.getPageSize());
        map.put("replyPostId", replyPostId);
        
        //封装每页显示的数据
        List<Reply> replyList = postMapper.selectReplyByPage(map);
        pageBean.setLists(replyList);
        
		return pageBean;
	}

	public int updateReplyStatus(int replyId) {
		logger.info("***PostServiceImpl类,updateReplyStatus的方法***");
		return postMapper.updateReplyStatus(replyId);
	}

	public int updatePostStatus(int postId) {
		logger.info("***PostServiceImpl类,updatePostStatus的方法***");
		return postMapper.updatePostStatus(postId);
	}
	// 点赞+-1
	public int addOrSubReplyLike(int likePostId, int likePostUserId, int likeReplyId, int likeFromUserId,
			int likeByUserId) {
		logger.info("***PostServiceImpl类,addOrSubReplyLike的方法***");
		if (null == postMapper.selectLike(likePostId, likePostUserId, likeReplyId, likeFromUserId, likeByUserId)) {
			postMapper.insertLike(likePostId, likePostUserId, likeReplyId, likeFromUserId, likeByUserId);
			return 1; // 点赞+1
		}else {
			postMapper.deleteLike(likePostId, likePostUserId, likeReplyId, likeFromUserId, likeByUserId);
			return 2; // 点赞-1
		}
	}

	public int updateReplyLike(int replyId, int flag) {
		logger.info("***PostServiceImpl类,updateReplyLike的方法***");
		if (flag == 1) {
			postMapper.addReplyLike(replyId);
		}else if(flag == 2) {
			postMapper.subReplyLike(replyId);
		}
		return 0;
	}
	// 举报
	public int insertReport(int postId, int postUserId, int reportUserId, String postTitle) {
		logger.info("***PostServiceImpl类,insertReport的方法***");
		return postMapper.insertReport(postId, postUserId, reportUserId, postTitle);
	}

	public Integer selectReport(int postId) {
		logger.info("***PostServiceImpl类,selectReport的方法***");
		return postMapper.selectReport(postId);
	}

	public int addOrSubCollect(int postId, int postUserId, int userId) {
		logger.info("***PostServiceImpl类,addOrSubCollect的方法***");
		if (null == postMapper.selectCollect(postId, userId)) {
			postMapper.insertCollect(postId, postUserId, userId);
			return 1; // 收藏+1
		}else {
			postMapper.deleteCollect(postId, postUserId, userId);
			return 2; // 收藏-1
		}
	}

	public String selectCollect(int postId, int userId) {
		logger.info("***PostServiceImpl类,selectCollect的方法***");
		if (null == postMapper.selectCollect(postId, userId)) {
			return "未收藏";
		}else {
			return "已收藏"; 
		}
	}

	public int insertMsg(int fromUserId, int byUserId, String msgName, String msgContent, String msgStatus) {
		logger.info("***PostServiceImpl类,insertMsg的方法***");
		return postMapper.insertMsg(fromUserId, byUserId, msgName, msgContent, msgStatus);
	}
	
	public int addPostBrowse(int postId) {
		logger.info("***PostServiceImpl类,addPostBrowse的方法***");
		return postMapper.addPostBrowse(postId);
	}

	public int addPostReply(int postId) {
		logger.info("***PostServiceImpl类,addPostReply的方法***");
		return postMapper.addPostReply(postId);
	}
	// 点赞！！
	public List<Like> selectLikeByUserId(int userId, int postId) {
		logger.info("***PostServiceImpl类,selectLikeByPostId的方法***");
		return postMapper.selectLikeByUserId(userId, postId);
	}
	public Integer[] selectNotLike(int userId, int postId) {
		logger.info("***PostServiceImpl类,selectLikeByPostId的方法***");
//		return postMapper.selectLikeByUserId(userId, postId);
		List<Like> list = postMapper.selectLikeByUserId(userId, postId);
		System.out.println(list);
		List<Integer> replyList = list.stream().map(Like -> Like.getLikeReplyId()).collect(Collectors.toList());
		System.out.println(replyList);
		List<Reply> list2 = postMapper.selectReplyByPId(postId);
		List<Integer> replyList2 = list2.stream().map(Reply -> Reply.getReplyId()).collect(Collectors.toList());
		System.out.println(replyList2);
		replyList2.removeAll(replyList);
		System.out.println(replyList2);
		Integer[] integers2 = replyList2.toArray(new Integer[0]);
		return integers2;
	}

	public List<Post> selectPostByUId(int userId) {
		logger.info("***PostServiceImpl类,selectPostByUId的方法***");
		return postMapper.selectPostByUId(userId);
	}

	public List<Collect> selectUserCollect(int userId) {
		logger.info("***PostServiceImpl类,selectUserCollect的方法***");
		return postMapper.selectUserCollect(userId);
	}
	
	public List<Post> selectUserCollectPost(int userId) {
		logger.info("***PostServiceImpl类,selectUserCollectPost的方法***");
		return postMapper.selectUserCollectPost(userId);
	}

	public Integer selectReportByUId(int postId, int reportUserId) {
		logger.info("***PostServiceImpl类,selectReportByUId的方法***");
		return postMapper.selectReportByUId(postId, reportUserId);
	}

	public Post selectPostDetail(int postId) {
		logger.info("***PostServiceImpl类,selectPostDetail的方法***");
		return postMapper.selectPostDetail(postId);
	}

	public String selectUserNameByPid(int postId) {
		logger.info("***PostServiceImpl类,selectUserNameByPid的方法***");
		return postMapper.selectUserNameByPid(postId);
	}

	public List<Post> selectPostByCur(int flag) {
		logger.info("***PostServiceImpl类,selectUserNameByPid的方法***");
		List<Post> list = null;
		if (flag == 1) {
			list = postMapper.selectPostByBrowse();
		}else if (flag == 2) {
			list = postMapper.selectPostByReply();
		}
		return list;
	}

	public int updateUserMoney(int replyFromUserId, int postReward) {
		logger.info("***PostServiceImpl类,updateUserMoney的方法***");
		return postMapper.updateUserMoney(replyFromUserId, postReward);
	}

	public int selectPostUserId(int postId) {
		logger.info("***PostServiceImpl类,selectPostUserId的方法***");
		return postMapper.selectPostUserId(postId);
	}


}
